# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: Jeff 'codemac' Mickey <jeff@archlinux.org>
# Contributor: Alexander Rødseth <rodseth@gmail.com>

pkgname=ejabberd
pkgver=23.04
pkgrel=1
pkgdesc="Jabber server written in Erlang"
arch=('loong64' 'x86_64')
url="https://www.ejabberd.im/"
license=("GPL")
depends=('expat' 'openssl' 'zlib' 'erlang-nox' 'pam' 'iproute2' 'erlang-unixodbc'
	 'libyaml' 'sqlite' 'gd')
makedepends=('git' 'rebar' 'hevea' 'texlive-bin' 'texlive-core' 'texlive-latexextra')
optdepends=('gsfonts: for captcha'
	    'imagemagick: for captcha')
backup=('etc/ejabberd/ejabberd.yml'
	'etc/ejabberd/ejabberdctl.cfg'
	'etc/logrotate.d/ejabberd')
options=(emptydirs)
source=("$pkgname-$pkgver.tar.gz::https://github.com/processone/ejabberd/archive/$pkgver.tar.gz"
	"base64url::git+https://github.com/dvv/base64url#commit=bf1360685da704c8453aba2268c4698c959d2ba4"
	"cache_tab::git+https://github.com/processone/cache_tab#commit=6da609074b855b904ab72e201c60f1a1e2c00b3f"
	"eimp::git+https://github.com/processone/eimp#commit=ec616de3762ce8498856b4fdadf8ba36045c6773"
	"ejabberd_po::git+https://github.com/processone/ejabberd-po#commit=c271992d07b20462e3c48f51cc8634e78488b354"
	"elixir::git+https://github.com/elixir-lang/elixir#commit=867da6f876c48279823a41a33030c95ef37379b2"
	"epam::git+https://github.com/processone/epam#commit=4986fbb15abb7f37798924025eab4bcf98a39d50"
	"eredis::git+https://github.com/wooga/eredis#commit=9ad91f149310a7d002cb966f62b7e2c3330abb04"
	"esip::git+https://github.com/processone/esip#commit=8ef94ff877e059fbdc93ac709b33b3c2efd2248b"
	"ezlib::git+https://github.com/processone/ezlib#commit=af37eb27aa0614db8b89110a4b56806b7f3544c4"
	"fast_tls::git+https://github.com/processone/fast_tls#commit=5aa1e02d1e91155a7b0027abde1af94147c54b82"
	"fast_xml::git+https://github.com/processone/fast_xml#commit=26d3247e7b7ff94a3bdcd30bad1193e2d86f511b"
	"fast_yaml::git+https://github.com/processone/fast_yaml#commit=24c5e30c0d73b9ef56e575327b8c334366a345f0"
	"idna::git+https://github.com/benoitc/erlang-idna#commit=6cff72747821110169ecfac871b0c69e5064afff"
	"jiffy::git+https://github.com/davisp/jiffy#commit=9ea1b35b6e60ba21dfd4adbd18e7916a831fd7d4"
	"jose::git+https://github.com/potatosalad/erlang-jose#commit=e0110a1afacde3011c8bb2a6f1c4ab9ea94bd4f4"
	"luerl::git+https://github.com/rvirding/luerl#commit=836d08ad3287d73d9c91cb11a590de15a1761027"
	"mqtree::git+https://github.com/processone/mqtree#commit=b87975f2c46ea44f3ed0dd484ae248aeea0c5d1e"
	"p1_acme::git+https://github.com/processone/p1_acme#commit=c1067aa3aace515553610ab78cb4ce5123fee207"
	"p1_mysql::git+https://github.com/processone/p1_mysql#commit=66a3c2ecbf7777a8c17bd5693eb9e442a0f9f1ff"
	"p1_oauth2::git+https://github.com/processone/p1_oauth2#commit=e1fbe216c9e0c1f85b44bf9750af608fcc2848d2"
	"p1_pgsql::git+https://github.com/processone/p1_pgsql#commit=166c191157502bcd9e4bd59585e133e36f9e8bcc"
	"p1_utils::git+https://github.com/processone/p1_utils#commit=ba816751674ab7df46978345ec4c0c84c0b44b0d"
	"pkix::git+https://github.com/processone/pkix#commit=9655992dc39e91fc8f5e237eea95d74ec9c01891"
	"rebar_elixir_plugin::git+https://github.com/processone/rebar_elixir_plugin#commit=10614dfef5d10b7071f7181858149259e50159f6"
	"sqlite3::git+https://github.com/processone/erlang-sqlite3#commit=0fc8ec99fa12b0acaa42b157b6a85ebf8d007387"
	"stringprep::git+https://github.com/processone/stringprep#commit=11fe965934be988f5c6c9ded9affa870f3e5057a"
	"stun::git+https://github.com/processone/stun#commit=3bf0daee4fa1c40d90169dd3a446602e9793266f"
	"unicode_util_compat::git+https://github.com/benoitc/unicode_util_compat.git#commit=38d7bc105f51159e8ea3279c40121db9db1e652f"
	"xmpp::git+https://github.com/processone/xmpp#commit=122e1b03ec20014410189cd369a996f63ac406d8"
	"yconf::git+https://github.com/processone/yconf#commit=af756beb3dac1e1f2e1242edd899f0a26557afa6"
	"$pkgname.logrotate"
	"sysuser.conf")
sha256sums=('6ff1d41a1ff6261a0c846193647d8ec143e82142859bf1cfdc62299022ceb2ad'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '31780cac78736d285e46f445f8c8463a70f2aeb2683280c259129db11832ddd2'
            '68de8fd4df6e4f0e21a241dcd2b7075bdff495876646dd726c7054bf5780e3c6')

prepare() {
  cd "$srcdir/$pkgname-$pkgver"
  mkdir -p "$srcdir/$pkgname-$pkgver/deps"
  for i in \
    base64url cache_tab eimp ejabberd_po elixir epam eredis esip ezlib fast_tls fast_xml fast_yaml idna jiffy jose luerl mqtree p1_acme p1_mysql p1_oauth2 p1_pgsql p1_utils pkix rebar_elixir_plugin sqlite3 stringprep stun unicode_util_compat xmpp yconf \
    ; do
    rm -rf "$srcdir/$pkgname-$pkgver/deps/$i"
    mv "$srcdir"/$i "$srcdir/$pkgname-$pkgver/deps"
  done
  sed -i "s|git describe --tags 2>/dev/null|echo $pkgver|" configure.ac
}

build() {
  cd "$srcdir/$pkgname-$pkgver"
  [ -x configure ] || ./autogen.sh
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --sbindir=/usr/bin \
    --localstatedir=/var \
    --enable-all
  make -j1
}

package() {
  cd "$srcdir/$pkgname-$pkgver"
  make DESTDIR="$pkgdir" install

  sed -i \
    -e "s|$pkgdir||g" \
    -e "s|Group=.*|Group=jabber|g" \
    -e "s|User=.*|User=jabber|g" \
    ejabberd.service
  sed -i -e "s|$pkgdir||g" "$pkgdir"/usr/bin/ejabberdctl

  install -Dm0644 ejabberd.service "$pkgdir"/usr/lib/systemd/system/ejabberd.service

  install -Dm04750 deps/epam/priv/bin/epam "$pkgdir"/usr/lib/ejabberd-$pkgver/priv/bin/epam
  install -d "$pkgdir/var/lib/$pkgname"
  install -D -m0644 "$srcdir/$pkgname.logrotate" "$pkgdir/etc/logrotate.d/$pkgname"
  chmod ug+r "$pkgdir/etc/$pkgname/"*
  chmod a+rx "$pkgdir/usr/bin/ejabberdctl" "$pkgdir/usr/lib/ejabberd-$pkgver/priv/bin/captcha.sh"
  rm -rf "$pkgdir/var/lock"
  install -Dm644 "$srcdir"/sysuser.conf "$pkgdir"/usr/lib/sysusers.d/ejabberd.conf
  install -Dm644 tools/ejabberdctl.bc "$pkgdir"/usr/share/bash-completion/completions/ejabberdctl

  # workaround
  ln -s mod_configure.beam "$pkgdir"/usr/lib/ejabberd-$pkgver/ebin/configure.beam
  rm -f "$pkgdir"/usr/bin/{elixir,iex,mix}

  echo -e "\n\n# home dir workaround\nHOME=/var/lib/ejabberd" >>"$pkgdir"/etc/ejabberd/ejabberdctl.cfg

  # /usr/lib/ejabberd symlink follows to ejabberd version
  ln -s ejabberd-$pkgver "$pkgdir"/usr/lib/ejabberd

  # permissions
  chown -R 17:17 "$pkgdir"/var/{lib,log}/ejabberd
  chown root:17 \
    "$pkgdir"/etc/ejabberd/ejabberd.yml \
    "$pkgdir"/etc/ejabberd/ejabberdctl.cfg \
    "$pkgdir"/etc/ejabberd \
    "$pkgdir"/usr/lib/ejabberd/priv/bin/epam

  # fix eimp
  chmod a+x "$pkgdir"/usr/lib/eimp-*/priv/bin/eimp
}

# vim:set ts=2 sw=2 et:
